package com.zhongyouyun.other.task;

import com.zhongyouyun.admin.model.JobModel;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class BaseTask {

    @Autowired
    private Scheduler scheduler;

    //添加任务
    public String addJob(JobModel job) {
        try {
            JobKey jobKey = new JobKey(job.getServiceName(), "group");
            JobDetail jobDetail = JobBuilder.newJob(BaseJob.class)
                    .withIdentity(jobKey)
                    .usingJobData("businessParam", job.getRemark())
                    .usingJobData("className", job.getServiceClass())
                    .usingJobData("methodName", job.getMethodName())
                    .build();
            TriggerKey triggerKey = new TriggerKey(job.getServiceName(), "group");
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(triggerKey)
                    .withSchedule(CronScheduleBuilder.cronSchedule(job.getCron()).withMisfireHandlingInstructionDoNothing())
                    .forJob(jobDetail)
                    .build();
            JobDetail jobDetailOld = scheduler.getJobDetail(jobKey);
            Trigger triggerOld = scheduler.getTrigger(triggerKey);
            if (triggerOld != null) {
                scheduler.rescheduleJob(triggerKey, trigger);
            } else {
                if (jobDetailOld == null) {
                    scheduler.scheduleJob(jobDetail, trigger);
                } else {
                    scheduler.scheduleJob(trigger);
                }
            }
        } catch (Exception e) {
            return "0";
        }
        return "1";
    }


    //暂停任务
    public void pauseJob(JobModel job) {
        //调度器为空
        if (scheduler == null) {
            return;
        }
        try {
            //触发器和任务都要暂停
            scheduler.pauseTrigger(new TriggerKey(job.getServiceName(), "group"));
            scheduler.pauseJob(new JobKey(job.getServiceName(),"group"));
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    //恢复调度器
    public void resumeJob(JobModel job) {
        //
        if (scheduler == null) {
            return;
        }
        //
        try {
            scheduler.resumeJob(new JobKey(job.getServiceName(), "group"));
            scheduler.resumeTrigger(new TriggerKey(job.getServiceName(), "group"));
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    //删除任务
    public void deleteJob(JobModel job) {
        if (scheduler == null) {
            return;
        }
        //JobKey对象
        JobKey jobKey = new JobKey(job.getServiceName(), "group");
        //触发器
        TriggerKey triggerKey = TriggerKey.triggerKey(job.getServiceName(), "group");
        try {
            // 停止触发器
            scheduler.pauseTrigger(triggerKey);
            // 移除触发器
            scheduler.unscheduleJob(triggerKey);
            //删除任务
            scheduler.deleteJob(jobKey);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    //修改任务
    public void alterJob(JobModel oldJob,JobModel newJob) {
           deleteJob(oldJob);
           addJob(newJob);
    }
}
